//
//  RlzjRootWindow.m
//  rui_lian_zhi_jia
//
//  Created by 代佳宏 on 2019/9/24.
//  Copyright © 2019 华宇瑞联. All rights reserved.
//

#import "FSRootWindow.h"

#define Biz_TabBar_Button_Width     76
#define Biz_TabBar_Button_Height    74
#define Biz_Tabar_Item_Count        5

static BOOL handleURL(NSURL * url,
                      NSString ** pScheme,
                      NSString ** pHost,
                      NSString ** pPath,
                      NSDictionary ** pParameters) {
    if(!url || !url.scheme || !url.host)
        return NO;
    
    NSMutableDictionary * mutableParameters = [NSMutableDictionary dictionary];
    NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&"];
    
    if(url.query && 0 < url.query) {
        
        NSScanner* scanner = [[NSScanner alloc] initWithString:url.query];
        
        while (![scanner isAtEnd]) {
            NSString* pairString = nil;
            [scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];
            [scanner scanCharactersFromSet:delimiterSet intoString:NULL];
            
            NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
            
            if (kvPair.count == 2) {
                NSString* key = [[kvPair objectAtIndex:0] stringByRemovingPercentEncoding];
                NSString* value = [[kvPair objectAtIndex:1] stringByRemovingPercentEncoding];
                [mutableParameters setObject:value forKey:key];
            }
        }
    }
    
    *pScheme = [url.scheme copy];
    *pHost = [url.host copy];
    *pPath = [url.path copy];
    *pParameters = [NSDictionary dictionaryWithDictionary:mutableParameters];
    
    return YES;
}
@interface FSRootWindow ()<UITabBarControllerDelegate>

@end

@implementation FSRootWindow

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_WHITE;
    self.delegate = self;
    
    NSDictionary * tabBarItemTitleAttribute = @{NSForegroundColorAttributeName :
                                                    COLOR_TABBAR_TINT,
                                                NSFontAttributeName :
                                                    [UIFont fontWithName:Biz_DEFAULT_REGULAR_FONT_NAME
                                                                    size:9.0f]};
    NSDictionary * tabBarItemTitleAttribute1 = @{NSForegroundColorAttributeName :
                                                     COLOR_TABBAR_TINT1,
                                                 NSFontAttributeName :
                                                     [UIFont fontWithName:Biz_DEFAULT_REGULAR_FONT_NAME
                                                                     size:9.0f]};
    
    UIImage * tabBarHomeImage = [[UIImage imageNamed:@"tabbar1"]
                                 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * tabBarHomeSelectedImage = [[UIImage imageNamed:@"tabbarS1"]
                                         imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    FSHomeRootVC * homeVC1 = [[FSHomeRootVC alloc] init];
    UINavigationController *homeNaviVC1 = [[UINavigationController alloc] initWithRootViewController:homeVC1];
    homeNaviVC1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页"
                                                           image:tabBarHomeImage
                                                   selectedImage:tabBarHomeSelectedImage];
    [homeNaviVC1.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute
                                          forState:UIControlStateNormal];
    [homeNaviVC1.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute1
                                          forState:UIControlStateSelected];
    
    
    
    UIImage * tabBarClassificationImage = [[UIImage imageNamed:@"tabbar2"]
                                    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * tabBarClassificationSelectedImage = [[UIImage imageNamed:@"tabbarS2"]
                                            imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       
    FSReportRootVC * ReportVC2 = [[FSReportRootVC alloc] init];
    UINavigationController * ReportNaviVC2 = [[UINavigationController alloc] initWithRootViewController:ReportVC2];
    ReportNaviVC2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"报告"
                                                             image:tabBarClassificationImage
                                                     selectedImage:tabBarClassificationSelectedImage];
    [ReportNaviVC2.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute
                                            forState:UIControlStateNormal];
    [ReportNaviVC2.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute1
                                            forState:UIControlStateSelected];
    
    
    
    UIImage * tabBarSearchGoodsImage = [[UIImage imageNamed:@"tabbar3"]
                                    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * tabBarSearchGoodsSelectedImage = [[UIImage imageNamed:@"tabbarS3"]
                                            imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       
    FSChatRootVC * ChatVC3 = [[FSChatRootVC alloc] init];
    UINavigationController * ChatNaviVC3 = [[UINavigationController alloc] initWithRootViewController:ChatVC3];
    ChatNaviVC3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"互动"
                                                             image:tabBarSearchGoodsImage
                                                     selectedImage:tabBarSearchGoodsSelectedImage];
    [ChatNaviVC3.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute
                                            forState:UIControlStateNormal];
    [ChatNaviVC3.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute1
                                            forState:UIControlStateSelected];
    ChatNaviVC3.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    
    
    
    
    
    UIImage * tabBarScanImage = [[UIImage imageNamed:@"tabbar4"]
                                 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * tabBarScanSelectedImage = [[UIImage imageNamed:@"tabbarS4"]
                                         imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    FSequipmentRootVC * equipmentVC4 = [[FSequipmentRootVC alloc] init];
    UINavigationController * equipmentNaviVC4 = [[UINavigationController alloc] initWithRootViewController:equipmentVC4];
    equipmentNaviVC4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设备"
                                                          image:tabBarScanImage
                                                  selectedImage:tabBarScanSelectedImage];
    [equipmentNaviVC4.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute
                                         forState:UIControlStateNormal];
    [equipmentNaviVC4.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute1
                                         forState:UIControlStateSelected];
    
   
    UIImage * tabBarManageImage = [[UIImage imageNamed:@"tabbar5"]
                                   imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * tabBarManageSelectedImage = [[UIImage imageNamed:@"tabbarS5"]
                                           imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    FSPersonalCenterRootVC * memberVC5 = [[FSPersonalCenterRootVC alloc] init];
    UINavigationController * memberNaviVC5 = [[UINavigationController alloc] initWithRootViewController:memberVC5];
    memberNaviVC5.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的"
                                                            image:tabBarManageImage
                                                    selectedImage:tabBarManageSelectedImage];
    [memberNaviVC5.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute
                                           forState:UIControlStateNormal];
    [memberNaviVC5.tabBarItem setTitleTextAttributes:tabBarItemTitleAttribute1
                                           forState:UIControlStateSelected];
    
    
    self.viewControllers = @[homeNaviVC1,ReportNaviVC2,ChatNaviVC3, equipmentNaviVC4, memberNaviVC5];
    self.selectedViewController = homeNaviVC1;
    
    //自定义tabbar
    [self.tabBar setShadowImage:[[UIImage alloc] init]];
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:COLOR_WHITE
                                                       size:CGSizeMake(1.0f, 1.0f)]];
    CALayer *shadowLine = [CALayer layer];
    shadowLine.backgroundColor = [UIColor colorWithRed:213.0/255
                                                 green:213.0/255
                                                  blue:213.0/255
                                                 alpha:1.0].CGColor;
    shadowLine.frame = CGRectMake(0, 0, CGRectGetWidth(self.tabBar.frame), 0.5);
    [self.tabBar.layer addSublayer:shadowLine];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UINavigationController *)viewController{
    //    if(self.viewControllers[1] == viewController ||self.viewControllers[2] == viewController||self.viewControllers[3] == viewController) {
    //        if(nil == [BizCoreData shareInstance].userInfo) { //未登录
    //            [self routerEventWithName:kBiz_Global_Event_Redirect_To_Login dataInfo:nil];
    //            return NO;
    //        }
    //    }
    
    return YES;
}

@end
